package com.literaryfamily.literaryfamily.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.literaryfamily.literaryfamily.exceptions.BestRuntimeException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AlipayMsgConfig {
	// TODO: 当前为开发阶段硬编码密钥，部署时请改为配置或环境变量读取

	@Value("${alipay.app-id}")
	private String appId;
	// TODO: 当前为开发阶段硬编码密钥，部署时请改为配置或环境变量读取

	@Value("${alipay.private-key}")
	private String privateKey;
	// TODO: 当前为开发阶段硬编码密钥，部署时请改为配置或环境变量读取

	@Value("${alipay.public-key}")
	private String alipayPublicKey;
	// TODO: 当前为开发阶段硬编码密钥，部署时请改为配置或环境变量读取

	@Value("${alipay.gateway}")
	private String gateway;

	@Bean
	public AlipayClient getAlipayClient() {
		try {
			AlipayConfig alipayConfig = new AlipayConfig();
			alipayConfig.setAppId(appId);
			alipayConfig.setPrivateKey(privateKey);
			alipayConfig.setAlipayPublicKey(alipayPublicKey);
			alipayConfig.setServerUrl(gateway);
			return new DefaultAlipayClient(alipayConfig);
		} catch (AlipayApiException e) {
			throw new BestRuntimeException("支付宝环境初始化失败!");
		}
	}
}